package org.jaaksi.demo.animator;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * ObjectAnimator extends ValueAnimator
 * 利用属性动画实现旋转、缩放、透明度变化
 */
public class NormalAnimation extends Activity {

    private ImageView imageView;
    private ValueAnimator rotationY;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    public void pause(View view) {
        if (rotationY == null)
            return;
        if (!rotationY.isRunning())
            return;
        if (rotationY.isPaused()) { // 暂停
            rotationY.resume();
        } else {
            rotationY.pause();
        }
    }

    public void rotateyAnimRun(final View view) {
        //  rotationY
//        rotationY = ObjectAnimator.ofFloat(imageView, "rotationY", 0, 360)
//                .setDuration(4000);
        rotationY = ValueAnimator.ofFloat(0, 360)
            .setDuration(4000);
        // 重复次数
        rotationY.setRepeatCount(2);
        // 重复类型，RESTART 动画结束后，立刻从初始状态再执行一次动画
//        rotationY.setRepeatMode(ValueAnimator.RESTART);
        //REVERSE 动画结束后，先动画逆回原来的状态，再从初始状态再执行一次动画
        rotationY.setRepeatMode(ValueAnimator.REVERSE);
        rotationY.addPauseListener(new Animator.AnimatorPauseListener() {
            @Override
            public void onAnimationPause(Animator animation) {
                toast("onAnimationPause");
            }

            @Override
            public void onAnimationResume(Animator animation) {
                toast("onAnimationResume");
            }
        });
        rotationY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 动画执行过程中调用
                Log.i("fcy", "------------onAnimationUpdate");
                float value = (Float) animation.getAnimatedValue();
                // 动画更新时，设置透明度从0到1.0
                imageView.setAlpha(value / 360);
//                imageView.setRotationY(value);
                imageView.setScaleX(value / 360);
                imageView.setScaleY(value / 360);
            }
        });
        rotationY.start();
    }

    private void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
